/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package szachy;

import java.io.Serializable;
import java.rmi.RemoteException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Robert
 */
public class Pion extends Figura implements Serializable{
    private static final long serialVersionUID = 1L;
    public Pion(char kolor, String nazwa, String rodzaj){
        this.kolor=kolor;
        iloscRuchu=1;
        this.nazwa=nazwa;
        this.rodzaj=rodzaj;
    }
public boolean mozliwyRuch(int fromX, int fromY, int toX, int toY,IGra g){
        boolean answer = true;
        try {
            //najpierw sprawdzamy warunek początkowy - pion na starcie może wykonać ruch o 2!
            if (g.ZwrocFigure(fromX, fromY).PobierzKolor() == 'c' && fromX == 1 && Math.abs(fromX - toX) == 2 && fromY == toY) {
                if (g.CzyZajete(fromX + 1, fromY) || g.CzyZajete(fromX + 2, fromY)) {
                    answer = false;
                }
            } else if (g.ZwrocFigure(fromX, fromY).PobierzKolor() == 'j' && fromX == 6 && Math.abs(fromX - toX) == 2 && fromY == toY) {
                if (g.CzyZajete(fromX - 1, fromY) || g.CzyZajete(fromX - 2, fromY)) {
                    answer = false;
                    //teraz ruch do przodu o 1
                }
            } else if (g.ZwrocFigure(fromX, fromY).PobierzKolor() == 'c' && toX - fromX == 1 && fromY == toY) {
                if (g.CzyZajete(fromX + 1, fromY)) {
                    answer = false;
                }
            } else if (g.ZwrocFigure(fromX, fromY).PobierzKolor() == 'j' && fromX - toX == 1 && fromY == toY) {
                if (g.CzyZajete(fromX - 1, fromY)) {
                    answer = false;
                }
            } else {
                answer = false;
            }
        } catch (RemoteException ex) {
            Logger.getLogger(Pion.class.getName()).log(Level.SEVERE, null, ex);
        }
        return answer;
}
public boolean mozliwoscBicia(int fromX, int fromY, int toX, int toY,IGra g){
        try {
            if (g.ZwrocFigure(toX, toY).CoTo().equals("krol")) {
                return false;
            }
        } catch (RemoteException ex) {
            Logger.getLogger(Pion.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            if (g.ZwrocFigure(fromX, fromY).PobierzKolor() == 'c' && toX - fromX == 1 && Math.abs(fromY - toY) == 1) {
                return true;
            } else if (g.ZwrocFigure(fromX, fromY).PobierzKolor() == 'j' && fromX - toX == 1 && Math.abs(fromY - toY) == 1) {
                return true;
            }
        } catch (RemoteException ex) {
            Logger.getLogger(Pion.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
}
}
